#include "database.h"

int Database::createDatabase()
{
    char* errMsg = nullptr;

    // 创建数据库
    int rc = sqlite3_open("/home/nebulau/data/ibed-server/ibed-server.db", &db);
    if (rc) {
        std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
        return rc;
    } else {
        std::cout << "Opened database successfully" << std::endl;
    }

    // 创建用户表
    const char* createUserTableSQL = R"(
        CREATE TABLE IF NOT EXISTS users (
            user_id INTEGER PRIMARY KEY AUTOINCREMENT,
            account TEXT UNIQUE NOT NULL,
            username TEXT NOT NULL,
            avatar_path TEXT,
            phone TEXT,
            birthday TEXT,
            bio TEXT,
            gender TEXT CHECK(gender IN ('male', 'female', 'other')),
            region TEXT,
            email TEXT UNIQUE NOT NULL,
            password TEXT NOT NULL
        );
    )";

    rc = sqlite3_exec(db, createUserTableSQL, nullptr, nullptr, &errMsg);
    CHECK_SQLITE(rc, SQLITE_OK, "Failed to create users table");
    
    // 创建图像表
    const char* createImageTableSQL = R"(
        CREATE TABLE IF NOT EXISTS images (
            image_id INTEGER PRIMARY KEY AUTOINCREMENT, 
            image_name TEXT UNIQUE NOT NULL,          
            size INTEGER NOT NULL,                    
            path TEXT NOT NULL,                     
            thumbnail_path TEXT NOT NULL,          
            document_id INTEGER,                      
            user_id INTEGER,                           
            FOREIGN KEY(user_id) REFERENCES users(user_id) 
        );
    )";

    rc = sqlite3_exec(db, createImageTableSQL, nullptr, nullptr, &errMsg);
    CHECK_SQLITE(rc, SQLITE_OK, "Failed to create images table");

    std::cout << "Tables created successfully!" << std::endl;

    return rc;
}


/**
 * 函数名：registerUser
 * 说明：注册用户信息
 * 入参：account
 *      username
 *      avatarPath
 *      phone
 *      birthday
 *      bio
 *      gender
 *      region
 *      email
 *      password
 * 返回值：账号存在
 *          用户名存在
 *          邮箱存在
 *          注册成功
 */
int Database::registerUser(const std::string &account, const std::string &username, const std::string &avatarPath, const std::string &phone, const std::string &birthday, const std::string &bio, const std::string &gender, const std::string &region, const std::string &email, const std::string &password)
{
    const char* paramInsertSQL = "INSERT INTO users (account, username, avatar_path, phone, birthday, bio, gender, region, email, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
    sqlite3_stmt* stmt = nullptr;
    
    int rc = sqlite3_prepare_v2(db, paramInsertSQL, -1, &stmt, 0);
    
    if (rc == SQLITE_OK) {
        // 绑定参数
        sqlite3_bind_text(stmt, 1, account.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 2, username.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 3, avatarPath.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 4, phone.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 5, birthday.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 6, bio.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 7, gender.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 8, region.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 9, email.c_str(), -1, SQLITE_STATIC);
        sqlite3_bind_text(stmt, 10, password.c_str(), -1, SQLITE_STATIC);
        
        // 执行
        rc = sqlite3_step(stmt);
        
        if (rc != SQLITE_DONE) {
            std::cerr << "注册信息语句执行错误: " << sqlite3_errmsg(db) << std::endl;
            return -1;  /*注册信息写入数据库失败*/
        } else {
            std::cout << "注册信息插入成功" << std::endl;
        }
        
        sqlite3_finalize(stmt);
    } else {
        std::cerr << "注册信息准备语句失败: " << sqlite3_errmsg(db) << std::endl;
    }
    return 0;
}

std::map<std::string, std::string> Database::loginUser(const std::string &phone, const std::string &password)
{
    return std::map<std::string, std::string>();
}

std::map<std::string, std::string> Database::getUserInfo(int userId)
{
    return std::map<std::string, std::string>();
}

bool Database::updateUserInfo(int userId, const std::map<std::string, std::string> &updatedInfo)
{
    return false;
}

bool Database::deleteUser(int userId)
{
    return false;
}

int Database::insertImage(const std::string &imageName, int size, const std::string &path, const std::string &thumbnailPath, int documentId, int userId)
{
    return 0;
}

bool Database::deleteImage(int imageId)
{
    return false;
}

std::map<std::string, std::string> Database::getImage(int imageId)
{
    return std::map<std::string, std::string>();
}

bool Database::updateImageInfo(int imageId, const std::map<std::string, std::string> &updatedInfo)
{
    return false;
}
